package com.jl.basicframework.ui.recyclerView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import me.leefeng.promptlibrary.PromptDialog;

/***
 * @author 作者:姜澜
 * 时间:2022/3/11
 * 邮箱:316879104@pp.com
 * 类简述:原则上所有的RecyclerViewAdapter都需要继承这个类
 */

public abstract class BaseRecyclerViewAdapterAdapter <M, B extends ViewDataBinding> extends RecyclerView.Adapter {

    /**
     * activity上下文
     */
    protected Context mContext;

    /**
     * recycler承载的数据
     */
    protected List<M> items;

    /**
     * dataBinding
     */
    protected B binding;

    /**
     * 是否需要返回为位置
     */
    private boolean isReturnPosition = false;

    public BaseRecyclerViewAdapterAdapter(Context mContext, List<M> items) {
        this.mContext = mContext;
        this.items = items;
    }

    public BaseRecyclerViewAdapterAdapter(Context mContext, List<M> items, boolean isReturnPosition) {
        this.mContext = mContext;
        this.items = items;
        this.isReturnPosition = isReturnPosition;
    }

    /**
     * 获取当前适配器中的数据列表
     */
    public List<M> getItems(){
        return items;
    }

    /**
     * 添加数据并刷新
     */
    public void addData(M data,int position){
        items.add(position,data);
        notifyItemInserted(position);
        notifyItemRangeChanged(position,items.size()-position);
    }

    /**
     * 清除数据并刷新
     */
    public void removeData(int position){

        items.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,items.size()-position);
    }


    /**
     * 由于RecyclerView的onBindViewHolder()方法，只有在getItemViewType()返回类型不同时才会调用，这点是跟ListView的getView()方法不同的地方，
     * 所以如果想要每次都调用onBindViewHolder()刷新item数据，就要重写getItemViewType()，让其返回position，否则很容易产生数据错乱的现象。
     * @param position
     * @return position
     */
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    protected void onBindItem(B binding, M item){}

    protected void onBindItem(B binding, M item,int position){}

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        B binding = DataBindingUtil.getBinding(holder.itemView);
        if (isReturnPosition){
            //返回position值
            this.onBindItem(binding,this.items.get(position),position);
        }else {
            //不返回position值
            this.onBindItem(binding, this.items.get(position));
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //初始化dataBinding
        binding = DataBindingUtil.inflate(
                LayoutInflater.from(mContext),
                getLayoutResId(viewType),
                parent,
                false);

        return new BaseBindingViewHolder(binding.getRoot());
    }

    @Override
    public int getItemCount() {
        return this.items.size();
    }

    /**
     *
     * @param viewType
     * @return
     */
    protected abstract @LayoutRes int getLayoutResId(int viewType);
}
